package com.xsky.logic.equiptype;

import java.util.List;
import java.util.UUID;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.xsky.database.dao.impl.BaseEntityDaoImpl;
import com.xsky.operation.vo.EquipType;
public class EquipTypeLogic
{
	public String add(String xmlDoc)//增加
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		XStream xs = new XStream(new DomDriver());
		xs.alias("item", EquipType.class);
		EquipType entity = (EquipType)xs.fromXML(xmlDoc);
		List rootList = baseDao.findByHql("from EquipType model where model.parentId='"+0+"'");
		if(rootList.size()==0)
		{
			EquipType root = new EquipType();
			root.setTypeName("根节点");
			root.setParentId("0");
			root.setId(UUID.randomUUID().toString().substring(0,31));
			baseDao.insertEntity(root);
			entity.setId(UUID.randomUUID().toString().substring(0,31));
			entity.setParentId(root.getId());
			baseDao.insertEntity(entity);
		}
		else
		{
			if(entity.getParentId()==null||"".equals(entity.getParentId()))
			{
				EquipType tcd = (EquipType)rootList.get(0);
				entity.setParentId(tcd.getId());
			}
			entity.setId(UUID.randomUUID().toString().substring(0,31));
			baseDao.insertEntity(entity);
		}
		return "新增类型成功";
	}
	public String delete(String id)//删除一个文件栏目
	{
		String hql = "delete EquipType model where model.id='"+id+"'";
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		baseDao.execute(hql);
		baseDao.execute("delete EquipType model where model.parentId='"+id+"'");
		return "删除类型成功";
	}
	public String update(String xmlDoc)//更新一个文件栏目
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		XStream xs = new XStream(new DomDriver());
		xs.alias("item", EquipType.class);
		EquipType ft = (EquipType)xs.fromXML(xmlDoc);
		baseDao.updateEntity(ft);
		return "更新类型成功";
	}
	public String genTree(String id)//获取某个节点的部门树
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		EquipType entity = new EquipType();
		entity = (EquipType)baseDao.findByPrimaryKey(entity, "id", id);
		String childXml = "<item id=\""+entity.getId()+"\" parentId=\""+entity.getParentId()+"\"  typeName=\""+entity.getTypeName()+"\">";
		List fileTitleList = baseDao.findByHql("from EquipType model where model.parentId='"+id+"'");
		for(int i = 0; i < fileTitleList.size(); i++)
		{
			EquipType childEntity = (EquipType)fileTitleList.get(i);
			childXml+=genTree(childEntity.getId());
		}
		childXml+="</item>";
		return childXml;
	}
	public String genUpper(String typeId)
	{
		String upperName= "";
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		EquipType equipType = new EquipType();
		equipType = (EquipType)baseDao.findByPrimaryKey(equipType, "id", typeId);
		if(equipType==null)
		{
			
		}
		else
		{
			upperName = equipType.getTypeName();
			String thisparentId = equipType.getParentId();
			if("0".equals(thisparentId))
			{
			}
			else
			{
				upperName+="->"+genUpper(thisparentId);
			}
		}
		return upperName;
	}
	public String genUppers(String parentId)
	{
		String typeName = "";
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		EquipType equipType = new EquipType();
		equipType = (EquipType)baseDao.findByPrimaryKey(equipType, "id", parentId);
		if(equipType==null)
		{
			return "";
		}
		else
		{
			typeName = equipType.getTypeName();
			String thisparentId = equipType.getParentId();
			if("0".equals(thisparentId))
			{
				return "";
			}
			else
			{
				typeName+="->"+genUpper(thisparentId);
			}
		}
		return typeName;
	}
	public String genTreeRootId()
	{
		String id="0";
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		List list = baseDao.findByHql("from EquipType model where model.parentId='0'");
		for(int i = 0; i < list.size(); i++)
		{
			EquipType childEntity = (EquipType)list.get(i);
			id = childEntity.getId();
		}
		return id;
	}
	public String isEmpty(String id)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		List lmList = baseDao.findByHql("from EquipType model where model.parentId='"+id+"'");
		if(lmList.size()>0)
		{
			return "false";
		}
		return "true";
	}
}
